package com.company.biz.user.action;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.biz.user.service.ClientService;
import com.company.user.req.AddClientReq;
import com.company.user.req.ClientIdReq;
import com.company.user.req.ClientIdsReq;
import com.company.user.req.ClientListPageReq;
import com.company.user.resp.ClientInfoResp;
import com.company.user.entity.UserEntity;
import com.company.user.vo.GeneralResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Api(description = "客户信息管理")
@RestController
@RequestMapping("/client")
public class ClientController extends BaseController {

    @Resource
    private ClientService clientService;

    @ApiOperation(value = "获取客户列表分页")
    @RequestMapping(value = "/getClientListPage", method = RequestMethod.POST)
    public GeneralResult<Page<ClientInfoResp>> getClientListPage(@RequestBody ClientListPageReq req) {
        Page<ClientInfoResp> clientListPage = clientService.getClientListPage(req);
        return GeneralResult.genSuccessResult(clientListPage);
    }


    @ApiOperation(value = "添加客户信息")
    @RequestMapping(value = "/addClient", method = RequestMethod.POST)
    public GeneralResult<String> addClient(@RequestBody AddClientReq req) {
        UserEntity userEntity = getUserEntity();
        boolean b = clientService.addClient(req, userEntity);
        return GeneralResult.genSuccessResult(b ? "添加成功" : "添加失败");
    }


    @ApiOperation(value = "通过客户id获取客户信息")
    @RequestMapping(value = "/getClientByClientId", method = RequestMethod.POST)
    public GeneralResult<ClientInfoResp> getClientByClientId(@RequestBody ClientIdReq req) {
        ClientInfoResp client = clientService.getClientByClientId(req.getClientId());
        return GeneralResult.genSuccessResult(client);
    }

    @ApiOperation(value = "修改客户信息")
    @RequestMapping(value = "/updateClientByClientId", method = RequestMethod.POST)
    public GeneralResult<String> updateClientByClientId(@RequestBody ClientInfoResp req) {
        boolean b = clientService.updateClientByClientId(req);
        return GeneralResult.genSuccessResult(b ? "修改成功" : "修改失败");
    }

    @ApiOperation(value = "删除客户通过客户id")
    @RequestMapping(value = "/deleteClientByClientIds", method = RequestMethod.POST)
    public GeneralResult<String> deleteClientByClientIds(@RequestBody ClientIdsReq req) {
        boolean b = clientService.deleteClientByClientId(req.getClientIds());
        return GeneralResult.genSuccessResult(b ? "删除成功" : "删除失败");
    }

}
